package com.ymk.cmd;

import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.exception.MallException;
import com.ymk.bo.goods.GoodsBO;
import com.ymk.core.code.cmd.GoodsCmdCode;
import com.ymk.core.code.req.ReqCode;
import com.ymk.mq.MessageSender;
import com.ymk.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@Service
public class GoodsPatchCmd extends ACmd {

    @Override
    public CmdSign executeSop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        GoodsBO reqData = super.getData(sign, GoodsBO.class);

        if (ReqCode.UPDATE_STATUS.equals(sign.getReqCode())){
            GoodsBO goodsBO = new GoodsBO();
            goodsBO.setId(reqData.getId());
            goodsBO.setStatus(reqData.getStatus());
            this.goodsService.update(goodsBO);
            return respSign;
        }

        this.goodsService.update(reqData);
        return respSign;
    }

    @Override
    public CmdSign executeMop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        GoodsBO reqData = super.getData(sign, GoodsBO.class);

        if (ReqCode.UPDATE_STATUS.equals(sign.getReqCode())){
            GoodsBO goodsBO = new GoodsBO();
            goodsBO.setId(reqData.getId());
            goodsBO.setStatus(reqData.getStatus());
            this.goodsService.update(goodsBO);
            return respSign;
        }
        super.setModifiedData(sign,reqData);
        this.goodsService.update(reqData);

        this.messageSender.sendGoodsCatCacheSync(sign.getMchId(),sign);
        this.messageSender.sendGoodsCacheSync(reqData.getId(),sign);

        return respSign;
    }

    @Override
    public CmdSign verify(HttpServletRequest request, CmdSign sign) throws Exception {
        GoodsBO reqData = super.getData(sign, GoodsBO.class);
        if (Objects.isNull(reqData.getId())){
            throw new MallException("id为空");
        }

        return null;
    }

    @Override
    public String getCmdCode() {
        return GoodsCmdCode.GOODS_PATCH;
    }

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private MessageSender messageSender;
}